package com.runa.persistence.task;

import com.runa.monitor.comm.dto.Node;
import com.runa.persistence.StringUtil;
import lombok.Data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 2024/11/8 14:40
 * zhangYB
 */
@Data
public class RequestPendingTask {

    /**
     * 抄表 抄阀 抄酷特阀
     */
    private TaskTypeEnum type;

    private Node node;

    private static final Map<String, RequestPendingTask> taskMap = new ConcurrentHashMap<>();

    public static void addTask(String netSerialNo, RequestPendingTask task) {
        taskMap.put(netSerialNo, task);
    }

    public static TaskTypeEnum getTaskType(String netSerialNo) {
        if (netSerialNo != null) {
            RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
            if (null != requestPendingTask) {
                return requestPendingTask.getType();
            }
        }
        return null;
    }

    public static RequestPendingTask getTask(String netSerialNo) {
        return taskMap.get(netSerialNo);
    }


    public RequestPendingTask() {
    }

    public RequestPendingTask(TaskTypeEnum type) {
        this.type = type;
    }

    public RequestPendingTask(TaskTypeEnum type, Node node) {
        this.type = type;
        this.node = node;
    }

    public static boolean isReadMeter(String netSerialNo) {
        if (StringUtil.isBlank(netSerialNo)) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            return TaskTypeEnum.READ_METER.equals(requestPendingTask.getType());
        }
        return false;
    }

    public static boolean isReadHyMeter(String netSerialNo) {
        return isReadMeter(netSerialNo, TaskTypeEnum.READ_HY_METER);
    }

    public static boolean isReadMeter(String netSerialNo, TaskTypeEnum taskType) {
        if (StringUtil.isBlank(netSerialNo)) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            return taskType.equals(requestPendingTask.getType());
        }
        return false;
    }


    public static boolean isPsValve(String netEquNo) {
        return isValve(netEquNo, TaskTypeEnum.READ_PS_VALVE, TaskTypeEnum.WRITE_PS_VALVE);
    }


    public static boolean isValve(String netSerialNo, TaskTypeEnum... taskTypes) {
        if (null == taskTypes || taskTypes.length == 0) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            for (TaskTypeEnum taskType : taskTypes) {
                if (taskType.equals(requestPendingTask.getType())) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isReadValve(String netSerialNo) {
        if (StringUtil.isBlank(netSerialNo)) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            return TaskTypeEnum.READ_VALVE.equals(requestPendingTask.getType());
        }
        return false;
    }

    public static boolean isKtValve(String netSerialNo) {
        if (StringUtil.isBlank(netSerialNo)) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            return TaskTypeEnum.READ_KT_VALVE.equals(requestPendingTask.getType())
                    || TaskTypeEnum.WRITE_KT_VALVE.equals(requestPendingTask.getType());
        }
        return false;
    }

    public static boolean isReadKtValve(String netSerialNo) {
        if (StringUtil.isBlank(netSerialNo)) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            return TaskTypeEnum.READ_KT_VALVE.equals(requestPendingTask.getType());
        }
        return false;
    }


    public static boolean isWkxValve(String netSerialNo) {
        if (StringUtil.isBlank(netSerialNo)) {
            return false;
        }
        RequestPendingTask requestPendingTask = taskMap.get(netSerialNo);
        if (null != requestPendingTask) {
            return TaskTypeEnum.READ_WKX_VALVE.equals(requestPendingTask.getType())
                    || TaskTypeEnum.WRITE_WKX_VALVE.equals(requestPendingTask.getType());
        }
        return false;
    }


}
